package cn.itcast.service.impl;

import cn.itcast.domain.Student;
import cn.itcast.mapper.StudentMapper;
import cn.itcast.service.StudentService;
import cn.itcast.utils.MapperFactory;
import cn.itcast.utils.TransactionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class StudentServiceImpl implements StudentService {
    //查询所有学生
//    public List<Student> findAll(int currentPage, int pageSize) {
//        SqlSession sqlSession = null;
//        List<Student> userList = null;
//        try {
//            sqlSession = MapperFactory.getSqlSession();
//            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//            PageHelper.startPage(currentPage,pageSize);
//            userList = mapper.findAll();
//            TransactionUtil.commit(sqlSession);
//        } catch (Exception e) {
//            TransactionUtil.rollback(sqlSession);
//            e.printStackTrace();
//        } finally {
//            TransactionUtil.close(sqlSession);
//        }
//
//        return userList;
//
//    }


    @Override
    public Page selectByPage(Integer currentPage, Integer pageSize) {
        SqlSession sqlSession = null;
        Page page = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            page = PageHelper.startPage(currentPage, pageSize);
            mapper.findAll();
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


        return page;
    }
}
